﻿using System.Collections.Generic;
using System.Linq;
using Harvest.Api.Model;
using Harvest.Api.Model.Internal;
using Harvest.Api.Types;

namespace Harvest.Api.ExtensionMethods
{

    public static class ExpenseCategoryExtensions
    {

        public static ExpenseCategory ToExpenseCategory(this ExpenseCategoryItem entity)
        {
            var result = new ExpenseCategory
            {
                Deactivated = entity.Deactivated,
                Id = entity.Id.HasValue ? entity.Id.Value : (int?)null,
                Name = entity.Name,
                UnitName = entity.UnitName,
                UnitPrice = entity.UnitPrice.HasValue ? entity.UnitPrice.Value : (double?)null
            };
            return result;
        }


        public static ExpenseCategoryItem ToExpenseCategoryItem(this ExpenseCategory entity)
        {

            var id = new NullableInt();
            if (entity.Id.HasValue) id = entity.Id.Value;

            var unitPrice = new NullableDouble();
            if (entity.UnitPrice.HasValue) unitPrice = entity.UnitPrice.Value;

            var result = new ExpenseCategoryItem
            {
                Deactivated = entity.Deactivated,
                Id = id,
                Name = entity.Name,
                UnitName = entity.UnitName,
                UnitPrice = unitPrice
            };
            return result;
        }


        public static List<ExpenseCategory> ToExpenseCategories(this IEnumerable<ExpenseCategoryItem> entity)
        {
            var result = entity.Select(x => x.ToExpenseCategory()).ToList();
            return result;
        }


        public static List<ExpenseCategoryItem> ToExpenseCategoriesItem(this IEnumerable<ExpenseCategory> entity)
        {
            var result = entity.Select(x => x.ToExpenseCategoryItem()).ToList();
            return result;
        }
    }

}
